﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class UserControls_TH_ChiTietGiaoDich : System.Web.UI.UserControl
{
    // edit mode by default is false
    private bool editMode = false;
    private DataSet objDataSet;
    // set up the form
    protected void Page_PreRender(object sender, EventArgs e)
    {
        // check if we must display order details       
        if (Session["AdminOrderID"] != null)
        {
            // fill constituent controls with data
            PopulateControls();
            // set edit mode
        }
        else
            // Hide
            this.Visible = false;
    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    // populate the form with data
    private void PopulateControls()
    {
        // obtain order ID from the session
        string orderId = Session["AdminOrderID"].ToString();
        // obtain order info
        DonDatHangBUS OrdersAccess =
            new DonDatHangBUS(TGDDConfiguration.DbConnectionString);
        objDataSet = OrdersAccess.GetInfoDonDatHang(int.Parse(orderId));
        DataRow row = objDataSet.Tables[0].Rows[0];
        DonDatHangDTO orderInfo = new DonDatHangDTO();
        orderInfo.Id = int.Parse(row["id"].ToString());
        orderInfo.ThanhTien = long.Parse(row["ThanhTien"].ToString());
        orderInfo.NgayDatHang = DateTime.Parse(row["NgayDatHang"].ToString());

        if (!row["NgayGiaoHang"].ToString().Equals(""))
        {
            orderInfo.NgayGiaoHang = DateTime.Parse(row["NgayGiaoHang"].ToString());
            dateShippedTextBox.Text = orderInfo.NgayGiaoHang.ToString();
        }
        else
        {
            dateShippedTextBox.Text = "";
        }
        orderInfo.XacNhan = bool.Parse(row["XacNhan"].ToString());
        orderInfo.DaGiao = bool.Parse(row["DaGiao"].ToString());
        orderInfo.GhiChu = row["GhiChu"].ToString();
        orderInfo.TenKhachHang = row["TenKhachHang"].ToString();
        orderInfo.DiaChiNguoiNhan = row["DiaChiNguoiNhan"].ToString();
        // populate labels and text boxes with order info
        orderIdLabel.Text = "Hiển thị đơn đặt hàng #" + orderId;
        totalAmountLabel.Text = String.Format("{0:#,##0;Nothing}", orderInfo.ThanhTien) + " VNĐ";
        dateCreatedTextBox.Text = orderInfo.NgayDatHang.ToString();
        //dateShippedTextBox.Text = orderInfo.NgayGiaoHang.ToString();
        verifiedCheck.Checked = orderInfo.XacNhan;
        completedCheck.Checked = orderInfo.DaGiao;
        commentsTextBox.Text = orderInfo.GhiChu;
        shippingAddressTextBox.Text = orderInfo.DiaChiNguoiNhan;
        // by default the Edit button is enabled, and the
        // Update and Cancel buttons are disabled
        // Decide which one of the other three buttons
        // should be enabled and which should be disabled
        // fill the data grid with order details
        objDataSet = OrdersAccess.GetDetailsDonDatHang(int.Parse(orderId));
        grid.DataSource = objDataSet.Tables[0];
        grid.DataBind();
    }
}
